﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.model;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class ShopTable
    {
        private static Logger _log = Logger.getLogger( typeof( ShopTable ).FullName );

        private static ShopTable _instance;

        private readonly Dictionary<int, L1Shop> _allShops = new Dictionary<int, L1Shop>();

        public static ShopTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new ShopTable();
            }
            return _instance;
        }

        private ShopTable()
        {/*TTOK*/
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                String sqlstr = "SELECT DISTINCT npc_id FROM shop";
                foreach ( DataRow item1 in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int npcId = Convert.ToInt32( item1["npc_id"] );
                    sqlstr = "SELECT * FROM shop WHERE npc_id=?1 ORDER BY order_id";
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = npcId;
                    DataTable dt = wrapper.GetDataTable( cmd );
                    List<L1ShopItem> sellingList = new List<L1ShopItem>();
                    List<L1ShopItem> purchasingList = new List<L1ShopItem>();
                    foreach ( DataRow item in dt.Rows )
                    {
                        int itemId = SqlUtils.toInt( item["item_id"] );
                        int sellingPrice = SqlUtils.toInt( item["selling_price"] );
                        int purchasingPrice = SqlUtils.toInt( item["purchasing_price"] );
                        int packCount = SqlUtils.toInt( item["pack_count"] );
                        packCount = packCount == 0 ? 1 : packCount;
                        if ( 0 <= sellingPrice )
                        {
                            L1ShopItem sitem = new L1ShopItem( itemId, sellingPrice, packCount );
                            sellingList.Add( sitem );
                        }
                        if ( 0 <= purchasingPrice )
                        {
                            L1ShopItem sitem = new L1ShopItem( itemId, purchasingPrice, packCount );
                            purchasingList.Add( sitem );
                        }
                    }
                    L1Shop shop = new L1Shop( npcId, sellingList, purchasingList );
                    _allShops[npcId] = shop;
                }
            }
        }

        /// <summary>
        /// NPCIDからお店情報を返す。
        /// </summary>
        /// <param name="npcId"></param>
        /// <returns></returns>
        public L1Shop get( int npcId )
        {/*TTOK*/
            L1Shop ret;
            _allShops.TryGetValue( npcId, out ret );
            return ret;
        }
    }
}
